package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.GwPlatformAccountRole;
import com.ruoyi.system.domain.GwPlatformRoleMenuApplet;
import com.ruoyi.system.domain.GwPlatformRoleMenuPc;
import com.ruoyi.system.domain.vo.AccountUserVO;
import com.ruoyi.system.domain.vo.SystemMenuVO;
import com.ruoyi.system.mapper.GwPlatformAccountRoleMapper;
import com.ruoyi.system.mapper.GwPlatformRoleMenuAppletMapper;
import com.ruoyi.system.mapper.GwPlatformRoleMenuPcMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GwPlatformRoleMapper;
import com.ruoyi.system.domain.GwPlatformRole;
import com.ruoyi.system.service.IGwPlatformRoleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 平台角色Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-26
 */
@Service
public class GwPlatformRoleServiceImpl implements IGwPlatformRoleService 
{
    @Resource
    private GwPlatformRoleMapper gwPlatformRoleMapper;
    @Resource
    private GwPlatformAccountRoleMapper gwPlatformAccountRoleMapper;
    @Resource
    private GwPlatformRoleMenuPcMapper menuPcMapper;
    @Resource
    private GwPlatformRoleMenuAppletMapper appletMapper;

    /**
     * 查询平台角色
     * 
     * @param id 平台角色主键
     * @return 平台角色
     */
    @Override
    public GwPlatformRole selectGwPlatformRoleById(String id)
    {
        GwPlatformRole role = gwPlatformRoleMapper.selectGwPlatformRoleById(id);
        if(role!=null){
            GwPlatformRoleMenuPc pc = new GwPlatformRoleMenuPc();
            pc.setPlatformRoleId(role.getId());
            List<GwPlatformRoleMenuPc> menuPcList = menuPcMapper.selectGwPlatformRoleMenuPcList(pc);
            role.setPcMenuList(menuPcList.stream().map(GwPlatformRoleMenuPc::getMenuId).collect(Collectors.toList()));

            GwPlatformRoleMenuApplet applet = new GwPlatformRoleMenuApplet();
            applet.setPlatformRoleId(role.getId());
            List<GwPlatformRoleMenuApplet> appletList = appletMapper.selectGwPlatformRoleMenuAppletList(applet);
            role.setAppletMenuList(appletList.stream().map(GwPlatformRoleMenuApplet::getMenuId).collect(Collectors.toList()));
        }

        return role;
    }

    /**
     * 查询平台角色列表
     * 
     * @param gwPlatformRole 平台角色
     * @return 平台角色
     */
    @Override
    public List<GwPlatformRole> selectGwPlatformRoleList(GwPlatformRole gwPlatformRole)
    {
        List<GwPlatformRole> roleList = gwPlatformRoleMapper.selectList(gwPlatformRole);
        roleList.forEach(k->{
            GwPlatformAccountRole accountRole = new GwPlatformAccountRole();
            accountRole.setPlatformRoleId(k.getId());
            List<GwPlatformAccountRole> accountRoleList = gwPlatformAccountRoleMapper.selectGwPlatformAccountRoleList(accountRole);
            k.setUserCount(accountRoleList.size());
        });
        return roleList;
    }

    /**
     * 新增平台角色
     * 
     * @param gwPlatformRole 平台角色
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertGwPlatformRole(GwPlatformRole gwPlatformRole)
    {
        gwPlatformRole.setId(IdUtils.fastSimpleUUID());
        gwPlatformRole.setCreateTime(DateUtils.getNowDate());
        gwPlatformRole.setCreateBy(SecurityUtils.getUserId()+"");
        int num = gwPlatformRoleMapper.insertGwPlatformRole(gwPlatformRole);
        if(!CollectionUtils.isEmpty(gwPlatformRole.getPcMenuList())){
            gwPlatformRole.getPcMenuList().forEach(k->{
                GwPlatformRoleMenuPc pc = new GwPlatformRoleMenuPc();
                pc.setMenuId(k);
                pc.setPlatformRoleId(gwPlatformRole.getId());
                menuPcMapper.insertGwPlatformRoleMenuPc(pc);
            });
        }
        if(!CollectionUtils.isEmpty(gwPlatformRole.getAppletMenuList())){
            gwPlatformRole.getAppletMenuList().forEach(k->{
                GwPlatformRoleMenuApplet applet = new GwPlatformRoleMenuApplet();
                applet.setMenuId(k);
                applet.setPlatformRoleId(gwPlatformRole.getId());
                appletMapper.insertGwPlatformRoleMenuApplet(applet);
            });
        }
        return num;
    }

    /**
     * 修改平台角色
     * 
     * @param gwPlatformRole 平台角色
     * @return 结果
     */
    @Override
    public int updateGwPlatformRole(GwPlatformRole gwPlatformRole)
    {
        gwPlatformRole.setUpdateTime(DateUtils.getNowDate());
        gwPlatformRole.setUpdateBy(SecurityUtils.getUserId()+"");
        int num = gwPlatformRoleMapper.updateGwPlatformRole(gwPlatformRole);
        menuPcMapper.deleteGwPlatformRoleMenuPcByPlatformRoleId(gwPlatformRole.getId());
        appletMapper.deleteGwPlatformRoleMenuAppletByPlatformRoleId(gwPlatformRole.getId());
        if(!CollectionUtils.isEmpty(gwPlatformRole.getPcMenuList())){
            gwPlatformRole.getPcMenuList().forEach(k->{
                GwPlatformRoleMenuPc pc = new GwPlatformRoleMenuPc();
                pc.setMenuId(k);
                pc.setPlatformRoleId(gwPlatformRole.getId());
                menuPcMapper.insertGwPlatformRoleMenuPc(pc);
            });
        }
        if(!CollectionUtils.isEmpty(gwPlatformRole.getAppletMenuList())){
            gwPlatformRole.getAppletMenuList().forEach(k->{
                GwPlatformRoleMenuApplet applet = new GwPlatformRoleMenuApplet();
                applet.setMenuId(k);
                applet.setPlatformRoleId(gwPlatformRole.getId());
                appletMapper.insertGwPlatformRoleMenuApplet(applet);
            });
        }

        return num;
    }

    /**
     * 批量删除平台角色
     * 
     * @param ids 需要删除的平台角色主键
     * @return 结果
     */
    @Override
    public int deleteGwPlatformRoleByIds(String[] ids)
    {
        return gwPlatformRoleMapper.deleteGwPlatformRoleByIds(ids);
    }

    /**
     * 删除平台角色信息
     * 
     * @param id 平台角色主键
     * @return 结果
     */
    @Override
    public int deleteGwPlatformRoleById(String id)
    {
        return gwPlatformRoleMapper.deleteGwPlatformRoleById(id);
    }
    /**
     * 变更角色状态
     * @Date 下午 1:39:14 2025/5/29
     * @param gwPlatformRole
     * @return 结果
     **/
    @Override
    public AjaxResult changeStatus(GwPlatformRole gwPlatformRole) {
        gwPlatformRole.setUpdateBy(SecurityUtils.getUserId()+"");
        gwPlatformRole.setUpdateTime(new Date());
        gwPlatformRoleMapper.updateGwPlatformRole(gwPlatformRole);
        return AjaxResult.success();
    }

    @Override
    public List<SystemMenuVO> getSystemAllMenu() {
        String[] menuNameArray= new String[4];
        menuNameArray[0]="公司治理";
        menuNameArray[1]="员工关系";
        menuNameArray[2]="合同管理";
        menuNameArray[3]="企业信息";

        List<SystemMenuVO> list =  gwPlatformRoleMapper.getSystemAllMenu(menuNameArray);
        return list;
    }

    @Override
    public List<AccountUserVO> getAccountByRole(GwPlatformRole gwPlatformRole) {

        return gwPlatformRoleMapper.getAccountByRole(gwPlatformRole);
    }

    @Override
    public int deleteAccount(GwPlatformRole gwPlatformRole) {
        if(gwPlatformRole==null || StringUtils.isBlank(gwPlatformRole.getId()) || StringUtils.isEmpty(gwPlatformRole.getUserId())){
            return 0;
        }
        GwPlatformAccountRole accountRole = new GwPlatformAccountRole();
        accountRole.setAccountId(gwPlatformRole.getUserId());
        accountRole.setPlatformRoleId(gwPlatformRole.getId());
        List<GwPlatformAccountRole> accountRoleList = gwPlatformAccountRoleMapper.selectGwPlatformAccountRoleList(accountRole);
        if(!CollectionUtils.isEmpty(accountRoleList)){
            accountRoleList.forEach(k->{
                gwPlatformAccountRoleMapper.deleteGwPlatformAccountRoleById(k.getId());
            });
        }

        return accountRoleList.size();
    }

    @Override
    public List<SystemMenuVO> getAppletAllMenu() {
        return gwPlatformRoleMapper.getAppletAllMenu();
    }

    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        List<GwPlatformRole> perms = gwPlatformRoleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (GwPlatformRole perm : perms)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }
}
